
(defun pronounce-number (n)
  ; defmacro for ..
  (cond ((= n 0) '())
        ((= n 1) '(One))
		((= n 2) '(Two))
		((= n 3) '(Three))
		((= n 4) '(Four))
		((= n 5) '(Five))
		((= n 6) '(Six))
		((= n 7) '(Seven))
		((= n 8) '(Eight))
		((= n 9) '(Nine))
		((= n 10) '(Ten))
		((= n 11) '(Eleven))
		((= n 12) '(Twelve))
		((= n 13) '(Thirteen))
		((= n 14) '(Fourteen))
		((= n 15) '(Fifteen))
		((= n 16) '(Sixteen))
		((= n 17) '(Seventeen))
		((= n 18) '(Eighteen))
		((= n 19) '(Nineteen))
		((= n 20) '(Twenty))
		((= n 30) '(Thirty))
		((= n 40) '(Forty))
		((= n 50) '(Fifty))
		((= n 60) '(Sixty))
		((= n 70) '(Seventy))
		((= n 80) '(Eighty))
		((= n 90) '(Ninety))
		((< n 100) (append (pronounce-number (- n (mod n 10))) (pronounce-number (mod n 10))))
		;;
		((and (= (mod n 100) 0) (< n 1000)) (append (pronounce-number (/ n 100)) '(hundred)))
		((< n 1000) (append (pronounce-number (- n (mod n 100))) '(and) (pronounce-number (mod n 100))))
		;;
		((and (= (mod n 1000) 0) (< n 1000000)) (append (pronounce-number (/ n 1000)) '(thousand)))
		((< n 1000000) (append (pronounce-number (- n (mod n 1000))) '(and) (pronounce-number (mod n 1000))))
		((= n 1000000) '(million))))

(defun length-first-list (items)
  (if (null items) 0
	  (length (string (car items)))))

(defun list-string-length (n)
  (defun length-inner (items init-length)
	(if (null items)
	    init-length
		(length-inner (cdr items) (+ init-length (length-first-list items)))))
  (length-inner (pronounce-number n) 0))

(defun p17 ()
  (loop for i to 999
		and n = 1 then (1+ n)
		and len = 0 then (+ len (list-string-length n))
		finally (return len)))
  
;; output number in words, just for compare with the simpier solution
(defun p17-w ()
  (mapcar
	(lambda (x) (format t "~a" x))
	(mapcar
	  'append
	(mapcar
	  'pronounce-number
	  (range 1 1000)))))
		
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun prompt-read (prompt)
  (format *query-io* "~d:" prompt)
  (force-output *query-io*)
  (read-line *query-io*))

;(format t "~a~%" (p17))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; much more simpier solution

(defun range (start end)
  (loop for i from start to end collect i)) 

" - print out all number;
  - use sed to remove space and hypens; sed 's/[ -]/g'
  - use wc to count the number;
  - probably need to minus addtional one for the 'hard return' character"
(defun ep17 ()
      (mapcar
        (lambda (x) (format t "~r" x))
	    (range 1 1000)))
(ep17)
